package com.lanou.renrenxin.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.renrenxin.R;
import com.lanou.renrenxin.mybomb.MyUser;
import com.lanou.renrenxin.onclicklistener.OnAddFriendItemClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zouhui on 16/3/21.
 */
public class AddFriendAdapter extends RecyclerView.Adapter<AddFriendAdapter.AddFriendViewHolder> {

    private Context context;
    private List<MyUser> data;
    private OnAddFriendItemClickListener onAddFriendItemClickListener;

    public void setOnAddFriendItemClickListener(OnAddFriendItemClickListener onAddFriendItemClickListener) {
        this.onAddFriendItemClickListener = onAddFriendItemClickListener;
    }

    public AddFriendAdapter(Context context) {
        this.context = context;
        data = new ArrayList<>();
    }

    @Override
    public AddFriendViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_recycle_addcontact,parent,false);
        AddFriendViewHolder holder = new AddFriendViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final AddFriendViewHolder holder, int position) {
        holder.headImg.setImageResource(R.mipmap.headimg);
        holder.username.setText(data.get(position).getUsername());
        holder.pos = position;
        holder.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onAddFriendItemClickListener.onItemClick(holder.pos);
            }
        });
    }


    @Override
    public int getItemCount() {
        return data != null ? data.size() : 0;
    }


    public  void refreshList(List<MyUser> list){
        data.clear();
        data.addAll(list);
        notifyDataSetChanged();
    }

    class AddFriendViewHolder extends RecyclerView.ViewHolder {
        private ImageView headImg;
        private TextView username;
        private Button add;
        private int pos = 0;

        public AddFriendViewHolder(View itemView) {
            super(itemView);
            headImg = (ImageView) itemView.findViewById(R.id.iv_item_recycle_addcontact_headImg);
            username = (TextView) itemView.findViewById(R.id.tv_item_recycle_addcontact_name);
            add = (Button) itemView.findViewById(R.id.btn_item_recycle_addcontact_add);
            pos = 0;
        }
    }
}
